package com.toe.forum.webapi.service.impl;

import com.github.pagehelper.PageHelper;
import com.toe.common.exception.ToeServiceException;
import com.toe.common.pojo.domain.ToeAuthenticationInfo;
import com.toe.common.restful.StateEnum;
import com.toe.forum.webapi.mapper.ForumReplyMapper;
import com.toe.pojo.dto.forum.RemarkAddDTO;
import com.toe.pojo.entity.forum.ForumRemark;
import com.toe.forum.webapi.mapper.ForumRemarkMapper;
import com.toe.forum.service.ForumRemarkService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.toe.pojo.vo.forum.RemarkDetailVO;
import com.toe.pojo.vo.forum.RemarkVO;
import com.toe.pojo.vo.user.UserUserVO;
import com.toe.sso.service.IUserInfoService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import com.toe.common.censor.TextCensor;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论表 服务实现类
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@Service
@Slf4j
public class ForumRemarkServiceImpl extends ServiceImpl<ForumRemarkMapper, ForumRemark> implements ForumRemarkService {

    @DubboReference
    private IUserInfoService dubboUserInfoService;

    @Autowired
    private ForumRemarkMapper forumRemarkMapper;

    @Autowired
    private ForumReplyMapper forumReplyMapper;

    @Override
    public void insert(RemarkAddDTO remarkAddDTO) {
        log.info("开始处理插入文章评论的业务!!!!!!!!!!!!!!!!!!");
        String remark = remarkAddDTO.getRemark();
        if (remark==null){
            throw new ToeServiceException(StateEnum.ERR_FORBIDDEN,"请输入评论内容!");
        }
        String violationReason = TextCensor.TextCensor(remark);
        if (violationReason!=null){
            throw new ToeServiceException(StateEnum.ERR_FORUM_VIOLATION,violationReason);
        }
        ForumRemark forumRemark = new ForumRemark();
        BeanUtils.copyProperties(remarkAddDTO,forumRemark);
        //获取当前用户的登录信息
        Object credentials = SecurityContextHolder.getContext().getAuthentication().getCredentials();
        ToeAuthenticationInfo authenticationInfo = (ToeAuthenticationInfo)credentials;
        Long userId = authenticationInfo.getId();
        UserUserVO user = dubboUserInfoService.findUserById(userId);
        String nickname = user.getNickname();
        String url = user.getAvatar();
        forumRemark.setUserId(userId);
        forumRemark.setNickname(nickname);
        forumRemark.setUrl(url);
        LocalDateTime date = LocalDateTime.now();
      //  Date date = Date.from(now.atZone(ZoneId.systemDefault()).toInstant());
        forumRemark.setCreateTime(date);
        forumRemark.setUpdateTime(date);
        forumRemark.setIsReport(0);
        forumRemark.setLabel(1);
        forumRemarkMapper.insert(forumRemark);
        log.info("该评论添加完成!!!!!!!!!!!");
    }

    @Override
    public void deleteById(long id) {
        log.info("开始执行删除评论的操作");
        forumRemarkMapper.deleteById(id);
        //同时删除回复评论
        forumReplyMapper.deleteByRemarkId(id);
    }

    @Override
    public List<RemarkVO> selectRemark(long articleId) {
        //查询出这篇文章下的所有一级评论
        List<RemarkVO> remarkVOS = forumRemarkMapper.selectLevelOneRemark(articleId);
        return remarkVOS;
    }

    @Override
    public List<RemarkVO> selectRemarkPage(long articleId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<RemarkVO> remarkVOS = forumRemarkMapper.selectLevelOneRemark(articleId);
        return remarkVOS;
    }

    @Override
    public int selectRemarkCount(long articleId) {
        int reply = forumReplyMapper.selectCountByArticleId(articleId);
        int remark = forumRemarkMapper.selectRemarkCount(articleId);
        return remark+reply;
    }

    @Override
    public List<RemarkDetailVO> selectAllRemark(long articleId) {
        List<RemarkDetailVO> remarkDetailVOS = forumRemarkMapper.selectAllRemark(articleId);
        return remarkDetailVOS;
    }

    @Override
    public List<RemarkDetailVO> selectAllRemarkPage(long articleId, long pageNum, long pageSize) {
        List<RemarkDetailVO> remarkDetailVOS = forumRemarkMapper.selectAllRemark(articleId);
        long frist = ((pageNum-1)*pageSize);
        List<RemarkDetailVO> remarkDetailVOS2 = new ArrayList<RemarkDetailVO>();
        for (long i = frist; i < frist+pageSize && i<remarkDetailVOS.size(); i++) {
            remarkDetailVOS2.add(remarkDetailVOS.get((int) i));
        }
        return remarkDetailVOS2;
    }

}
